package huawei.widget;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.AccessibilityDelegate;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.core.widget.TextViewCompat;
import o.fkr$d;
import o.fkr$g;
import o.fkr$i;
import o.fkr.b;
import o.fo;
import o.fpx;
import o.fqb;
import o.in;

public class HwCounterTextLayout extends LinearLayout {
    private int ʻ;
    private int ʼ;
    private int ʽ;
    private int ˊ;
    private c ˊॱ;
    private fpx ˋ;
    private TextView ˎ;
    private EditText ˏ;
    private d ˏॱ;
    private int ॱ;
    private int ॱॱ;
    private Animation ᐝ;

    class a extends AccessibilityDelegate {
        final /* synthetic */ HwCounterTextLayout ॱ;

        private a(HwCounterTextLayout hwCounterTextLayout) {
            this.ॱ = hwCounterTextLayout;
        }

        public void onInitializeAccessibilityEvent(View view, AccessibilityEvent accessibilityEvent) {
            super.onInitializeAccessibilityEvent(view, accessibilityEvent);
            accessibilityEvent.setClassName(HwCounterTextLayout.class.getSimpleName());
        }

        public void onPopulateAccessibilityEvent(View view, AccessibilityEvent accessibilityEvent) {
            super.onPopulateAccessibilityEvent(view, accessibilityEvent);
        }

        public void onInitializeAccessibilityNodeInfo(View view, AccessibilityNodeInfo accessibilityNodeInfo) {
            super.onInitializeAccessibilityNodeInfo(view, accessibilityNodeInfo);
            accessibilityNodeInfo.setClassName(HwCounterTextLayout.class.getSimpleName());
            if (this.ॱ.ˏ != null) {
                accessibilityNodeInfo.setLabelFor(this.ॱ.ˏ);
            }
            CharSequence ˊ = this.ॱ.ˊ();
            if (!TextUtils.isEmpty(ˊ)) {
                in.ˋ().ॱˊ(true);
                in.ˋ().ᐝ(ˊ);
            }
        }
    }

    class c implements TextWatcher {
        final /* synthetic */ HwCounterTextLayout ˏ;

        private c(HwCounterTextLayout hwCounterTextLayout) {
            this.ˏ = hwCounterTextLayout;
        }

        public void afterTextChanged(Editable editable) {
        }

        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
        }

        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            this.ˏ.ॱ(charSequence);
        }
    }

    class d implements AnimationListener {
        private boolean ˎ;
        final /* synthetic */ HwCounterTextLayout ˏ;

        private d(HwCounterTextLayout hwCounterTextLayout) {
            this.ˏ = hwCounterTextLayout;
        }

        public void onAnimationStart(Animation animation) {
            this.ˏ.ˎ.setTextColor(fo.ˋ(this.ˏ.getContext(), b.ॱ));
            this.ˏ.ˏ(this.ˎ);
        }

        public void onAnimationEnd(Animation animation) {
            TextViewCompat.ˎ(this.ˏ.ˎ, this.ˏ.ˊ);
            this.ˏ.ˏ(false);
            this.ˎ = false;
        }

        public void onAnimationRepeat(Animation animation) {
        }
    }

    public HwCounterTextLayout(Context context) {
        this(context, null);
    }

    public HwCounterTextLayout(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, fkr$d.ॱ);
    }

    public HwCounterTextLayout(Context context, AttributeSet attributeSet, int i) {
        super(fqb.ˋ(context), attributeSet, i);
        ॱ(super.getContext(), attributeSet, i);
    }

    private void ॱ(Context context, AttributeSet attributeSet, int i) {
        setOrientation(1);
        TypedArray obtainStyledAttributes = context.obtainStyledAttributes(attributeSet, fkr$i.ʼ, i, fkr$g.ˋ);
        this.ˊ = obtainStyledAttributes.getResourceId(fkr$i.ॱˊ, 0);
        this.ˋ = fpx.values()[obtainStyledAttributes.getInt(fkr$i.ˏॱ, 0)];
        this.ʽ = obtainStyledAttributes.getResourceId(fkr$i.ͺ, 0);
        this.ॱॱ = obtainStyledAttributes.getResourceId(fkr$i.ʻ, 0);
        this.ʻ = obtainStyledAttributes.getResourceId(fkr$i.ॱॱ, 0);
        this.ॱ = obtainStyledAttributes.getInteger(fkr$i.ˋॱ, -1);
        this.ʼ = obtainStyledAttributes.getResourceId(fkr$i.ʽ, 0);
        obtainStyledAttributes.recycle();
        if (getImportantForAccessibility() == 0) {
            setImportantForAccessibility(1);
        }
        setAccessibilityDelegate(new a());
        Resources resources = getResources();
        setPaddingRelative(resources.getDimensionPixelSize(o.fkr.c.ˋ), 0, resources.getDimensionPixelSize(o.fkr.c.ˎ), 0);
    }

    public void addView(View view, int i, LayoutParams layoutParams) {
        if (view instanceof EditText) {
            View linearLayout;
            ˎ((EditText) view);
            LayoutParams layoutParams2 = new LinearLayout.LayoutParams(layoutParams);
            if (this.ˋ == fpx.LINEAR) {
                linearLayout = new LinearLayout(getContext());
                linearLayout.setGravity(16);
                linearLayout.setMinimumHeight(getResources().getDimensionPixelSize(o.fkr.c.ˊ));
                layoutParams2.weight = 1.0f;
                linearLayout.setLayoutParams(layoutParams2);
            } else {
                linearLayout = new FrameLayout(getContext());
            }
            linearLayout.addView(view, 0, layoutParams2);
            super.addView(linearLayout, 0, layoutParams2);
            ˋ();
            return;
        }
        super.addView(view, i, layoutParams);
    }

    private void ˎ(EditText editText) {
        if (this.ˏ != null) {
            throw new IllegalArgumentException("We already have an EditText, can only have one");
        }
        this.ˏ = editText;
        this.ˏ.setImeOptions(this.ˏ.getImeOptions() | 33554432);
        if (this.ˋ == fpx.BUBBLE) {
            this.ˏ.setBackgroundResource(this.ʻ);
        } else {
            this.ˏ.setBackgroundResource(this.ʽ);
        }
    }

    private void ˋ() {
        this.ˎ = new TextView(this, getContext()) {
            final /* synthetic */ HwCounterTextLayout ˏ;

            protected void onVisibilityChanged(View view, int i) {
                super.onVisibilityChanged(view, i);
                if (this.ˏ.ˋ == fpx.BUBBLE) {
                    this.ˏ.ॱ();
                }
            }
        };
        TextViewCompat.ˎ(this.ˎ, this.ˊ);
        this.ˎ.setGravity(17);
        LayoutParams layoutParams = new LinearLayout.LayoutParams(-2, -2);
        if (this.ˋ == fpx.BUBBLE) {
            LayoutParams layoutParams2 = new FrameLayout.LayoutParams(layoutParams);
            layoutParams2.gravity = 8388693;
            layoutParams2.setMarginEnd(this.ˏ.getPaddingEnd());
            layoutParams2.bottomMargin = this.ˏ.getPaddingBottom();
            ((ViewGroup) this.ˏ.getParent()).addView(this.ˎ, layoutParams2);
            return;
        }
        layoutParams.gravity = 8388693;
        addView(this.ˎ, layoutParams);
    }

    private void ॱ() {
        int i = ((MarginLayoutParams) this.ˎ.getLayoutParams()).bottomMargin;
        if (this.ˎ.getVisibility() == 0) {
            i += this.ˎ.getHeight() + getResources().getDimensionPixelSize(o.fkr.c.ˏ);
        }
        this.ˏ.setPaddingRelative(this.ˏ.getPaddingStart(), this.ˏ.getPaddingTop(), this.ˏ.getPaddingEnd(), i);
    }

    public void setHint(CharSequence charSequence) {
        if (this.ˏ != null) {
            this.ˏ.setHint(charSequence);
            sendAccessibilityEvent(2048);
        }
    }

    public void setMaxLength(int i) {
        this.ॱ = i;
    }

    public void setError(CharSequence charSequence) {
        int i = 0;
        if (this.ˏ != null && this.ˎ != null) {
            int i2 = !TextUtils.isEmpty(charSequence) ? 1 : 0;
            this.ˎ.setText(charSequence);
            TextView textView = this.ˎ;
            if (i2 == 0) {
                i = 8;
            }
            textView.setVisibility(i);
            this.ˎ.animate().setInterpolator(new LinearInterpolator()).setDuration(50).alpha(i2 != 0 ? 1.0f : 0.0f).start();
            sendAccessibilityEvent(2048);
        }
    }

    private void ॱ(CharSequence charSequence) {
        if (this.ॱ == -1) {
            setError(null);
        } else if (charSequence instanceof Editable) {
            Editable editable = (Editable) charSequence;
            int length = editable.length();
            if (length > this.ॱ) {
                length = this.ˏ.getSelectionEnd();
                editable.delete(this.ॱ, editable.length());
                EditText editText = this.ˏ;
                if (length > this.ॱ) {
                    length = this.ॱ;
                }
                editText.setSelection(length);
                if (this.ᐝ == null) {
                    this.ᐝ = AnimationUtils.loadAnimation(getContext(), o.fkr.a.ˊ);
                    this.ˏॱ = new d();
                    if (this.ᐝ != null) {
                        this.ᐝ.setAnimationListener(this.ˏॱ);
                    }
                }
                this.ˏॱ.ˎ = true;
                this.ˏ.startAnimation(this.ᐝ);
            } else if (((float) length) >= ((float) this.ॱ) * 0.9f) {
                setError(length + " / " + this.ॱ);
            } else {
                setError(null);
            }
        }
    }

    private void ˏ(boolean z) {
        if (this.ˋ == fpx.BUBBLE) {
            this.ˏ.setBackgroundResource(z ? this.ॱॱ : this.ʻ);
            ॱ();
            return;
        }
        this.ˏ.setBackgroundResource(z ? this.ʼ : this.ʽ);
    }

    protected void onLayout(boolean z, int i, int i2, int i3, int i4) {
        super.onLayout(z, i, i2, i3, i4);
        if (this.ˏ != null) {
            if (this.ˋ == fpx.BUBBLE) {
                ViewGroup viewGroup = (ViewGroup) this.ˏ.getParent();
                this.ˏ.layout(0, 0, viewGroup.getWidth(), viewGroup.getHeight());
            }
            if (this.ˊॱ == null) {
                this.ˊॱ = new c();
                this.ˏ.addTextChangedListener(this.ˊॱ);
                this.ˏ.setText(this.ˏ.getText());
            }
        }
    }

    public CharSequence ˊ() {
        if (this.ˎ != null) {
            return this.ˎ.getText();
        }
        return null;
    }
}
